﻿//编辑对话框
var dlg = new MyAppDialog(600, 400);//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}



//删除
function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert(csms_lang.message_info_title, csms_lang.message_delete_choosedelete);
        return;
    }
    if (confirm(csms_lang.message_delete_confrim)) {
        var ids = "";
        for (var i = 0; i < row.length; i++) {
            ids += row[i].Id + ",";
        }

        $.ajax({
            type: "post",
            url: baseurl + "Manage/Role/Delete",
            data: { ids: ids },
            success: function (e) {
                if (e.Ret == "0") {
                    $("#datagrid").datagrid("reload");
                } else {
                    $.messager.alert(csms_lang.message_error, e.Msg);
                }
            }
        });
    }
}
$(document).ready(function () {

    //删除
    $("#btndel").click(function () { del(); });

    //添加
    $("#btnadd").click(function () {
        dlg.setSize(600, 400);
        dlg.setURL("新增角色", baseurl + "Manage/Role/Edit/0?" + Math.random());
        dlg.show();
    });
    //选择客户
    $(".selectcust").click(function () {
        dlg.setSize(700, 480);
        dlg.setURL("选择客户", baseurl + "Public/CustomerSelect?callback=setCustcode&select=single");
        dlg.show();
    });
    $("#Roletype").change(function () {
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        params.roletype = $("#Roletype").val();
        $("#datagrid").datagrid("load");
    });
    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        params.roletype = $("#Roletype").val();
        $("#datagrid").datagrid("load");
    });

    $("#searchkey").keydown(function (event) {
        if (event.keyCode == $.ui.keyCode.ENTER) {
            event.preventDefault();
            $("#btnquery").click();
            return false;
        }
    });

    LoadDatagrid();
    if (!canEdit) {
        $("#datagrid").datagrid('showColumn', 'op');
    }
});

function setCustcode(jsonstr) {
    var jsondata = JSON.parse(jsonstr);

    $("#custcode").html(jsondata.Customername);
    $("#custcode").attr("data-id", jsondata.Custcode);

    $("#btnquery").click();
}

//加载列表数据
function LoadDatagrid() {
    var $dg = $("#datagrid");
    $dg.datagrid({
        title: '角色列表',
        url: baseurl + 'Manage/Role/RoleListJson',
        fit: true,
        fitColumns: true,
        striped: true,
        nowrap: true,
        idField: 'Id',
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 45, 60, 75],
        rownumbers: true,
        singleSelect: false,
        queryParams: {
            s: "",
            roletype: $("#Roletype").val()
        },
        sortName: 'Roleid',
        sortOrder: 'asc',
        columns: [[
        { field: "Id", title: 'Id', width: 10, hidden: true },
        { field: "ck", title: '', width: 20, checkbox: true },
        //{ field: "Roletype", title: '角色类别', sortable: true, width: 100, align: 'center' },
        //{ field: "Custcode", title: '客户名称', sortable: true, width: 100, align: 'center' },
        { field: "Roleid", title: '角色代码', sortable: true, width: 100, align: 'center' },
        { field: "Rolename", title: '角色名称', sortable: true, width: 100, align: 'center' },
        { field: "Description", title: '角色描述', sortable: true, width: 200, align: 'center' },
        {
            field: "op", title: '操作', width: 60, align: 'center', formatter:
              function (value, rec) {
                  var op = "<div class='btn-group'>";

                  op += "<a href='#none' onclick='rolemenu(this)' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>菜单权限</a>"
                  //op += "<a href='#none' onclick='IslockedFun(this,\"open\")' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>启用</a>"
                  op += "<a href='#none' onclick='roleuser(this)' class='btn btn-mini btn-default' data-id='" + rec.Roleid + "'>查看用户</a>"
                  op += "</div>";
                  return op;

                  //return "<a href='#none' onclick='rolemenu(this)' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>菜单权限</a><a href='#none' onclick='rolemenu(this)' class='btn btn-mini btn-default' data-id='" + rec.Id + "'>查看用户</a>"
              }
        }
        ]],
        onLoadSuccess: function () {
            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
            $dg.datagrid("clearSelections");
        },
        onDblClickRow: function (rowIndex, row) {
            dlg.setSize(600, 400);
            dlg.setURL("编辑角色", baseurl + "Manage/Role/Edit/" + row.Id + "?" + Math.random());
            dlg.show();

        },
        //onSelect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //},
        //onUnselect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //}
    });
}

//菜单权限
function rolemenu(obj) {
    //  debugger;
    dlg.setSize(500, 650);
    dlg.setURL("菜单权限", baseurl + "Manage/Role/RoleMenu/" + $(obj).attr("data-id") + "?" + Math.random());
    dlg.show();
}
//查看用户
function roleuser(obj) {
    //  debugger;
    dlg.setSize(850, 500);
    dlg.setURL("查看用户", baseurl + "Manage/Role/RoleUser/" + $(obj).attr("data-id") + "?" + Math.random());
    dlg.show();
}
